/**
 * Created by Administrator on 2017/1/17.
 */

var MJ = require('./MJ');

/**
 * 玩家手牌
 * @param owner
 * @constructor
 */
function HandCards() {
    this.cards = {};   // 手牌{card:num}
    this.lastCard = 0;// 上次摸的牌
    this.initLastCard = 0;// 起手牌的最后一张牌

    ExObject.each(this.cards,function(card,value){

    });
}

HandCards.prototype = {
    // 初始化
    init: function (cardArr) {
        for (var i = 0, m = cardArr.length; i < m; ++i) {
            ExObject.addNumber(this.cards, cardArr[i], 1);
        }
        this.initLastCard = cardArr[cardArr.length - 1];
    },

    /**
     * 重置
     */
    reset: function() {
        this.cards = {};
        this.lastCard = 0;
        this.initLastCard = 0;
    },

    /**
     * 获取牌
     * @returns {{}|*}
     */
    getCards: function() {
        return this.cards;
    },

    /**
     * 获取牌张数
     * @param card
     * @returns {*|number}
     */
    getCardNum: function(card) {
        if (card) {
            return this.cards[card] || 0;
        }

        return ExObject.sumValue(this.cards);
    },

    /**
     * 是否为空，即没有牌
     * @returns {boolean}
     */
    isEmpty: function() {
        return this.getCardNum() <= 0;
    },

    /**
     * 获取上次摸的牌
     * @returns {number|*}
     */
    getLastNewCard: function() {
        return this.lastCard;
    },

    /**
     * 获取起手牌的最后一张牌
     * @returns {number|*}
     */
    getInitLastCard: function() {
        return this.initLastCard;
    },

    /**
     * 摸牌，将增加1张牌
     * @param card
     */
    addNewCard: function(card) {
        ExObject.addNumber(this.cards, card, 1);
        this.lastCard = card;
    },

    /**
     * 增加1张牌
     * @param card
     * @param num 数量，默认为1
     */
    addCard: function(card, num) {
        ExObject.addNumber(this.cards, card, num || 1);
    },

    /**
     * 删除1张牌
     * @param card
     */
    delCard: function(card) {
        var num = this.cards[card];
        if (num) {
            if (num <= 1) {
                delete this.cards[card];
            } else {
                this.cards[card] = num - 1;
            }
            return true;
        }
        return false;
    },

    /**
     * 出牌，将减少1张牌
     * @param card
     * @returns boolean 是否成功
     */
    play: function(card) {
        var num = this.cards[card];
        if (num) {
            if (num <= 1) {
                delete this.cards[card];
            } else {
                this.cards[card] = num - 1;
            }
            return true;
        }

        return false;
    },

    /**
     * 碰牌，将减少2张牌
     * @param card
     * @returns {boolean}
     */
    peng: function(card) {
        var num = this.cards[card];
        if (num < 2) {
            return false;
        }

        if (num == 2) {
            delete this.cards[card];
        } else {
            this.cards[card] = num - 2;
        }
        return true;
    },

    /**
     * 点杠，将减少3张牌，用自己手中的3张牌杠别人打出的牌
     * @param card
     * @returns {boolean}
     */
    dianGang: function(card) {
        var num = this.cards[card];
        if (num != 3) {
            return false;
        }
        delete this.cards[card];
        return true;
    },

    /**
     * 暗杠，将减少4张牌
     * @param card
     * @returns {boolean}
     */
    anGang: function(card) {
        var num = this.cards[card];
        if (num != 4) {
            return false;
        }
        delete this.cards[card];
        return true;
    },

    getAnGangCards: function() {
        var anGangCards = [];
        for (var card in this.cards) {
            if (this.cards[card] == 4) {
                anGangCards.push(+card);
            }
        }
        return anGangCards;
    },
};


exports.HandCards = HandCards;